import React, { Component } from 'react'
import { Switch, Route, withRouter } from 'react-router-dom'
import { connect } from 'react-redux'
import sess from './utils/sess';
import Main from './Main'
// import Detail from './views/Detail';
//搜索
import Search from './views/Search';
//微课
import Small from './views/Small'
//系列课
import Series from './views/Series'
//培训课
import Cultivate from './views/Cultivate'
//机构系列
import Organization from './views/Organization'
//已购
import AlreadyBuy from './views/AlreadyBuy'
//登录
import Login from './views/Login'

@withRouter
@connect(state => ({
  token: state.user.token || sess.get('token') || ''
}), null)
class App extends Component {
  allowPathname = ['/login']

  checkLogin = pathname => {
    // 指定要验证的pathname路由
    if (!this.allowPathname.includes(pathname)) {
      if (this.props.token == '') {
        this.props.history.push('/login')
      }
    }
  }

  componentDidMount() {
    this.checkLogin(this.props.location.pathname)
    this.props.history.listen(location => {
      this.checkLogin(location.pathname)
    })
  }

  render() {
    return (
      <>

        <Route exact path="/" component={Main} />
        {/* <Route path="/detail/:id" component={Detail} /> */}
        <Route path="/search" component={Search} />
        <Route path="/small" component={Small} />
        <Route path="/series" component={Series} />
        <Route path="/cultivate" component={Cultivate} />
        <Route path="/organization" component={Organization} />
        <Route path="/alreadyBuy" component={AlreadyBuy} />
        <Route path="/login" component={Login} />
      </>
    );
  }
}

export default App;
